﻿@inject IStringLocalizer<TablesFooterStatistics> Localizer
@inject IStringLocalizer<Foo> LocalizerFoo

<Table TItem="Foo" ShowFooter="true" class="footer-demo"
       IsPagination="true" PageItemsSource="@PageItemsSource" OnQueryAsync="@OnQueryAsync">
    <TableColumns>
        <TableColumn @bind-Field="@context.DateTime" Width="180" />
        <TableColumn @bind-Field="@context.Name" />
        <TableColumn @bind-Field="@context.Address" />
        <TableColumn @bind-Field="@context.Count" />
    </TableColumns>
    <TableFooter>
        <TableFooterCell Text="@Localizer["TablesFooterStatisticsTotal"]" colspan="3" Align="@Align" />
        <TableFooterCell Aggregate="@Aggregate" Field="@nameof(Foo.Count)" />
    </TableFooter>
</Table>
<div class="mt-3 btn-group">
    <Button Icon="fa-solid fa-align-left" Text="@Left" OnClick="() => Align = Alignment.Left" />
    <Button Icon="fa-solid fa-align-center" Text="@Center" OnClick="() => Align = Alignment.Center" />
    <Button Icon="fa-solid fa-align-right" Text="@Right" OnClick="() => Align = Alignment.Right" />
</div>
<div class="mt-3 btn-group">
    <Button Text="Sum" OnClick="() => Aggregate = AggregateType.Sum" />
    <Button Text="Average" OnClick="() => Aggregate = AggregateType.Average" />
    <Button Text="Count" OnClick="() => Aggregate = AggregateType.Count" />
    <Button Text="Min" OnClick="() => Aggregate = AggregateType.Min" />
    <Button Text="Max" OnClick="() => Aggregate = AggregateType.Max" />
</div>

@code {
    private static IEnumerable<int> PageItemsSource => new int[] { 2, 4, 10, 20 };

    /// <summary>
    /// Foo 类为Demo测试用，如有需要请自行下载源码查阅
    /// Foo class is used for Demo test, please download the source code if necessary
    /// https://gitee.com/LongbowEnterprise/BootstrapBlazor/blob/main/src/BootstrapBlazor.Shared/Data/Foo.cs
    /// </summary>
    [NotNull]
    private IEnumerable<Foo>? Items { get; set; }

    [NotNull]
    private string? Left { get; set; }

    [NotNull]
    private string? Center { get; set; }

    [NotNull]
    private string? Right { get; set; }

    private Alignment Align { get; set; }

    private AggregateType Aggregate { get; set; }

    /// <summary>
    ///
    /// </summary>
    protected override void OnInitialized()
    {
        base.OnInitialized();

        Items = Foo.GenerateFoo(LocalizerFoo);
        Left ??= "Left";
        Center ??= "Center";
        Right ??= "Right";
    }

    private Task<QueryData<Foo>> OnQueryAsync(QueryPageOptions options)
    {
        // 设置记录总数
        var total = Items.Count();

        // 内存分页
        var items = Items.Skip((options.PageIndex - 1) * options.PageItems).Take(options.PageItems).ToList();

        return Task.FromResult(new QueryData<Foo>()
        {
            Items = items,
            TotalCount = total,
            IsSorted = true,
            IsFiltered = true,
            IsSearch = true
        });
    }
}
